package com.cskaoyan.demo1;

import com.cskaoyan.demo1.bean.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.List;

public class JacksonApplication {
    public static void main(String[] args) throws JsonProcessingException {
        //writeValueAsString();
        readValue();
    }



    private static void writeValueAsString() throws JsonProcessingException {
        User user = User.builder()
                .name("张三")
                .age(18)
                .sex("男")
                .build();
        ObjectMapper objectMapper = new ObjectMapper();
        String userJsonStr = objectMapper.writeValueAsString(user);
        System.out.println(userJsonStr);

        String[] nameArray = {"张三","李四","王五"};
        String nameArrayJsonStr = objectMapper.writeValueAsString(nameArray);
        System.out.println(nameArrayJsonStr);

        List<String> list = Arrays.asList(nameArray);
        String listJsonStr = objectMapper.writeValueAsString(list);
        System.out.println(listJsonStr);
    }

    private static void readValue() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = "{\"name\":\"张三\",\"age\":18,\"sex\":\"男\"}";
        User user = objectMapper.readValue(jsonStr, User.class);
        System.out.println("user = " + user);
    }
}
